package cn.sxw.ld.module.member.controller;


import cn.sxw.ld.common.dto.R;
import cn.sxw.ld.module.member.pojo.Member;
import cn.sxw.ld.module.member.service.MemberService;
import cn.sxw.ld.module.pet.service.PetService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: SXW
 * @since: 2025年02月20日 11:18
 * @desc:
 */
@Slf4j
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;
    @Autowired
    private PetService petService;

    /**
     * 获取分页
     *
     * @param page
     * @param pageSize
     * @param name     查询参数
     * @param status   1 为空，2 为手机，3 为用户名
     * @return
     */
    @GetMapping("/page")
    public R<Page> Page(int page, int pageSize, String name, int status) {
        try {
            Page pageInfo = memberService.getPage(page, pageSize, name, status);
            return R.success(pageInfo);
        } catch (Exception e) {
            log.error("会员获取分页错误:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }

    }

    /**
     * 新增会员
     *
     * @param
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Member member) {
        try {
            if (memberService.checkSave(member)) {
                return R.success("1");
            }
            return R.success("0");
        } catch (Exception e) {
            log.error("新增会员错误:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }
    }

    /**
     * 修改会员信息
     *
     * @param member
     * @return
     */
    @PutMapping()
    public R<String> updata(@RequestBody Member member) {
        try {
            return memberService.updateById(member) ?
                    R.success("修改成功！") :
                    R.error("修改失败，请稍后再试~~");
        } catch (Exception e) {
            log.error("会员信息更新错误:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }
    }

    /**
     * 通过 id 查询会员信息
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Member> getById(@PathVariable Long id) {
        try {
            Member member = memberService.getById(id);
            if (member != null) {
                return R.success(member);
            }
            return R.error("没有查询到对应会员");
        } catch (Exception e) {
            log.error("会员信息查询错误:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }
    }

    @DeleteMapping
    public R<String> deleteMembers(@RequestParam List<String> ids) {
        try {
            return memberService.batchDeleteMemberAndPet(ids) ?
                    R.success("删除成功！") :
                    R.error("删除失败，请稍后再试~~");
        } catch (Exception e) {
            log.error("会员删除错误:'\n'{}", e.getMessage());
            return R.error("系统繁忙，请稍后再试~~");
        }
    }

}
